import { getKNSUrl } from '@/api/common'
import { ElMessage } from 'element-plus'

// CDAP资源代码与知网节资源代码映射
const resourceMapping = {
  Journal: 'JOURNAL',
  Newspaper: 'NEWSPAPER',
  Doctoral_Thesis: 'DISSERTATION',
  Master_Thesis: 'DISSERTATION',
  Case: '',
  Laws: '',
  Achievement: 'ACHIEVEMENTS',
  Conference: 'CONFERENCE',
  Standard: 'STANDARD',
  Patent: 'PATENT',
  Yearbook: 'ALMANAC',
}

export async function openKNS(resouceCode, fileName) {
  try {
    const res = await getKNSUrl(resourceMapping[resouceCode], fileName)
    if (res.data) {
      window.open(res.data)
    } else {
      ElMessage.error(res.errMessage)
    }
  } catch (error) {
    if (error.cause !== 'axios') {
      ElMessage.error(error.message)
    }
  }
}

/**
 * 打开知网作者
 * @param {*} authorCode 作者码
 */
export function openCnkiAuthor(authorCode) {
  window.open(`https://kns.cnki.net/kcms/detail/knetsearch.aspx?code=${authorCode}&sfield=au`)
}
